﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProcurementManagement.Infrstructre
{
    public class BaseRepository<T>:IRepository<T> where T:class,new()
    {
        ISqlSugarClient db;
        public BaseRepository(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddAsync(T entity)
        {
            return await db.Insertable(entity).ExecuteCommandAsync();
        }
        /// <summary>
        /// 批量真删除
        /// </summary>
        /// <param name="entity">要删除的实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DelRange(List<T> entity)
        {
            return db.Deleteable(entity).ExecuteCommand();
        }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns>返回集合</returns>
        public async Task<ISugarQueryable<T>> GetAll()
        {
            return db.Queryable<T>();
        }
        /// <summary>
        /// 没有异步获取所有数据
        /// </summary>
        /// <returns>返回集合</returns>
        public List<T> Query()
        {
            return db.Queryable<T>().ToList();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">要修改的实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdataAsync(T entity)
        {
            return await db.Updateable(entity).ExecuteCommandAsync();
        }
    }
}
